﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string CRM_Client_Add(CRM_Client model ,int ACID);


[OperationContract]
string CRM_Client_Update(CRM_Client model ,int ACID);


[OperationContract]
string CRM_ClientByClientID_Delete(Guid ClientID,int ACID);


[OperationContract]
CRM_Client CRM_ClientByClientID_Get(Guid ClientID,int ACID );


[OperationContract]
List<CRM_Client> CRM_Client_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加CRM_Client表数据
/// <summary>
/// 添加CRM_Client
/// </summary>
/// <param name="model">CRM_Client实体</param>
/// <returns>空或错误</returns>
public string CRM_Client_Add(CRM_Client model,int ACID)
{
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
if(model.IsDouble==null )
{
return "『供应商客户双身份』不能为空！";
}
if(string.IsNullOrEmpty(model.ClientName))
{
return "『客户全称』不能为空！";
}
if(string.IsNullOrEmpty(model.ShortName))
{
return "『客户用户名』不能为空！";
}
if(string.IsNullOrEmpty(model.Code))
{
return "『客户编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_Client.Count(p=>p.ClientID==model.ClientID)>0)
{
return "主键已存在！";
}
if(ctx.CRM_Client.Count(p=>p.Code==model.Code)>0)
{
return "客户编号已存在！";
}
ctx.CRM_Client.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加CRM_Client表数据事物用重构
/// <summary>
/// 添加CRM_Client事物用重构
/// </summary>
/// <param name="model">CRM_Client实体</param>
/// <returns>空或错误</returns>
private string CRM_Client_Add(CRM_Client model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
if(model.IsDouble==null )
{
return "『供应商客户双身份』不能为空！";
}
if(string.IsNullOrEmpty(model.ClientName))
{
return "『客户全称』不能为空！";
}
if(string.IsNullOrEmpty(model.ShortName))
{
return "『客户用户名』不能为空！";
}
if(string.IsNullOrEmpty(model.Code))
{
return "『客户编号』不能为空！";
}
if(ctx.CRM_Client.Count(p=>p.ClientID==model.ClientID)>0)
{
return "主键已存在！";
}
if(ctx.CRM_Client.Count(p=>p.Code==model.Code)>0)
{
return "客户编号已存在！";
}
ctx.CRM_Client.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改CRM_Client表数据
/// <summary>
/// 修改CRM_Client
/// </summary>
/// <param name="model">CRM_Client实体</param>
/// <returns>空或错误</returns>
public string CRM_Client_Update(CRM_Client model,int ACID)
{
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
if(model.IsDouble==null )
{
return "『供应商客户双身份』不能为空！";
}
if(string.IsNullOrEmpty(model.ClientName))
{
return "『客户全称』不能为空！";
}
if(string.IsNullOrEmpty(model.ShortName))
{
return "『客户用户名』不能为空！";
}
if(string.IsNullOrEmpty(model.Code))
{
return "『客户编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_Client.Count(p=>p.ClientID==model.ClientID)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
CRM_Client CRM_Clientmodel=ctx.CRM_Client.Single(p => p.ClientID==model.ClientID);
if(model.ClientID!=null || model.ClientID.Equals(Guid.Empty))
{
CRM_Clientmodel.ClientID=model.ClientID;
}
if(!string.IsNullOrEmpty(model.Code))
{
CRM_Clientmodel.Code=model.Code;
}
if(!string.IsNullOrEmpty(model.ShortName))
{
CRM_Clientmodel.ShortName=model.ShortName;
}
if(!string.IsNullOrEmpty(model.ZJM))
{
CRM_Clientmodel.ZJM=model.ZJM;
}
if(model.balance!=null )
{
CRM_Clientmodel.balance=model.balance;
}
if(model.suramt!=null )
{
CRM_Clientmodel.suramt=model.suramt;
}
if(model.credit!=null )
{
CRM_Clientmodel.credit=model.credit;
}
if(!string.IsNullOrEmpty(model.AreaCode))
{
CRM_Clientmodel.AreaCode=model.AreaCode;
}
if(model.IndustryCode!=null )
{
CRM_Clientmodel.IndustryCode=model.IndustryCode;
}
if(model.CooperationID!=null )
{
CRM_Clientmodel.CooperationID=model.CooperationID;
}
if(model.EmplID!=null || model.EmplID.Equals(Guid.Empty))
{
CRM_Clientmodel.EmplID=model.EmplID;
}
if(model.bankid!=null )
{
CRM_Clientmodel.bankid=model.bankid;
}
if(model.WorthID!=null )
{
CRM_Clientmodel.WorthID=model.WorthID;
}
if(model.SourceID!=null )
{
CRM_Clientmodel.SourceID=model.SourceID;
}
if(!string.IsNullOrEmpty(model.KindCode))
{
CRM_Clientmodel.KindCode=model.KindCode;
}
if(!string.IsNullOrEmpty(model.ClientName))
{
CRM_Clientmodel.ClientName=model.ClientName;
}
if(model.IsDouble!=null )
{
CRM_Clientmodel.IsDouble=model.IsDouble;
}
if(!string.IsNullOrEmpty(model.Adress))
{
CRM_Clientmodel.Adress=model.Adress;
}
if(model.Level!=null )
{
CRM_Clientmodel.Level=model.Level;
}
if(!string.IsNullOrEmpty(model.CoreBusiness))
{
CRM_Clientmodel.CoreBusiness=model.CoreBusiness;
}
if(!string.IsNullOrEmpty(model.WebSite))
{
CRM_Clientmodel.WebSite=model.WebSite;
}
if(!string.IsNullOrEmpty(model.Intro))
{
CRM_Clientmodel.Intro=model.Intro;
}
if(!string.IsNullOrEmpty(model.legal))
{
CRM_Clientmodel.legal=model.legal;
}
if(model.capital!=null )
{
CRM_Clientmodel.capital=model.capital;
}
if(!string.IsNullOrEmpty(model.taxid))
{
CRM_Clientmodel.taxid=model.taxid;
}
if(!string.IsNullOrEmpty(model.bankname))
{
CRM_Clientmodel.bankname=model.bankname;
}
if(!string.IsNullOrEmpty(model.bankNo))
{
CRM_Clientmodel.bankNo=model.bankNo;
}
if(model.fixedcheckDate!=null )
{
CRM_Clientmodel.fixedcheckDate=model.fixedcheckDate;
}
if(!string.IsNullOrEmpty(model.custom1))
{
CRM_Clientmodel.custom1=model.custom1;
}
if(!string.IsNullOrEmpty(model.custom2))
{
CRM_Clientmodel.custom2=model.custom2;
}
if(!string.IsNullOrEmpty(model.Remarks))
{
CRM_Clientmodel.Remarks=model.Remarks;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除CRM_Client表ByClientID数据
/// <summary>
/// 删除CRM_ClientByClientID
/// </summary>
/// <param name="ClientID">客户ID</param>
/// <returns>空或错误</returns>
public string CRM_ClientByClientID_Delete(Guid ClientID,int ACID)
{
if(ClientID==null || ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_Client.Count(p=>p.ClientID==ClientID)!=1)
{
return null;
}
CRM_Client model=ctx.CRM_Client.Single(p => p.ClientID== ClientID);
ctx.CRM_Client.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除CRM_Client表ByClientID数据[事物用重构]
/// <summary>
/// 删除CRM_ClientByClientID[事物用重构]
/// </summary>
/// <param name="ClientID">客户ID</param>
/// <returns>空或错误</returns>
private string CRM_ClientByClientID_Delete(Guid ClientID,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(ClientID==null || ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
if(ctx.CRM_Client.Count(p=>p.ClientID==ClientID)!=1)
{
return null;
}
CRM_Client model=ctx.CRM_Client.Single(p => p.ClientID== ClientID);
ctx.CRM_Client.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取CRM_Client表ByClientID数据
/// <summary>
/// 获取CRM_ClientByClientID
/// </summary>
/// <param name="ClientID">客户ID</param>
/// <returns>当前表</returns>
public CRM_Client CRM_ClientByClientID_Get(Guid ClientID,int ACID)
{
if(ClientID==null || ClientID.Equals(Guid.Empty))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.CRM_Client.Count(p=>p.ClientID==ClientID)!=1)
{
return null;
}
return ctx.CRM_Client.SingleOrDefault(p=>p.ClientID==ClientID);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取CRM_Client表数据
/// <summary>
/// 获取CRM_Client
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_Client></returns>
public List<CRM_Client> CRM_Client_Get(Expression<Func<CRM_Client, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_Client.ToList();
}
 else 
{
return ctx.CRM_Client.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_Client.Where(predicate).ToList();
}
 else 
{
return ctx.CRM_Client.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取CRM_Client表数据
/// <summary>
/// 获取CRM_Client
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_Client></returns>
public List<CRM_Client> CRM_Client_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return CRM_Client_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取CRM_Client表数据计数
/// <summary>
/// 获取CRM_Client表数据计数
/// </summary>
/// <returns>List<CRM_Client></returns>
public int CRM_Client_GetCount(Expression<Func<CRM_Client, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.CRM_Client.Count();

}
 else
{
return ctx.CRM_Client.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取CRM_Client分页表数据
/// <summary>
/// 获取CRM_Client分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<CRM_Client></returns>
public List<CRM_Client> CRM_Client_Getpage(int pageIndex, int pageSize,Expression<Func<CRM_Client, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_Client.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.CRM_Client.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.CRM_Client.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.CRM_Client.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改CRM_Client表数据
/// <summary>
/// 批量修改CRM_Client
/// </summary>
/// <param name="predicate">Expression<Func<CRM_Client, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<CRM_Client,CRM_Client>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int CRM_Client_batchUpdate(Expression<Func<CRM_Client, bool>> predicate,Expression<Func<CRM_Client, CRM_Client>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.CRM_Client.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改CRM_Client表数据事物用重构
/// <summary>
/// 批量修改CRM_Client事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<CRM_Client, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<CRM_Client,CRM_Client>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int CRM_Client_batchUpdate(Expression<Func<CRM_Client, bool>> predicate,Expression<Func<CRM_Client, CRM_Client>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.CRM_Client.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除CRM_Client表数据
/// <summary>
/// 批量删除CRM_Client
/// </summary>
/// <param name="predicate">Expression<Func<CRM_Client, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int CRM_Client_batchDel(Expression<Func<CRM_Client, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.CRM_Client.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除CRM_Client表数据事物用重构
/// <summary>
/// 批量删除CRM_Client事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<CRM_Client, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int CRM_Client_batchDel(Expression<Func<CRM_Client, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.CRM_Client.Delete(predicate);
return ok;
}
#endregion


}
}

